package com.cool.springcloud.controller;

import com.cool.springcloud.entity.PaymentEntity;
import com.cool.springcloud.service.OpenfeignPaymentService;
import com.cool.springcloud.utils.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @Author ifredomvip@gmail.com
 * @Date 2022/9/7 16:05
 * @Version 1.0.0
 * @Description
 **/
@Slf4j
@RestController
@RequestMapping("/consumer")
public class OpenfeignPayment {

    public static String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OpenfeignPaymentService openfeignPaymentService;


    @PostMapping("/openfeign/insertOne")
    public AjaxResult<Void> insertOne(@RequestBody PaymentEntity payment) {

        AjaxResult<Integer> ajaxResult = openfeignPaymentService.insertOne(payment);
        log.info("---------------" + ajaxResult);
        return new AjaxResult<>(200, "success");
    }
    @PostMapping("/openfeign/getById/{id}")
    public AjaxResult<Void> insertOne(@PathVariable Long id) {

        AjaxResult<PaymentEntity> ajaxResult = openfeignPaymentService.getOne(id);
        log.info("---------------" + ajaxResult);
        return new AjaxResult<>(200, "success");
    }

    @GetMapping("/openfeign/timeout")
    public AjaxResult<Void> timeoutRestful() {

        log.info("执行前-----------");
        AjaxResult<String> ajaxResult = openfeignPaymentService.timeoutRestful();
        log.info("执行后-----------" + ajaxResult);
        return new AjaxResult<>(200, "success");
    }

    @PostMapping("/openfeign/payment/insert")
    public AjaxResult<Void> callPaymentInsert(@RequestBody PaymentEntity payment) {

        log.info("执行前-----------");
        AjaxResult<Integer> ajaxResult = restTemplate.postForObject(PAYMENT_URL + "/payment/insertOne", payment, AjaxResult.class);
        log.info("执行后-----------" + ajaxResult);
        return new AjaxResult<>(200, "success");

    }



}
